package com.taiji.dianthus.controller.sms;

import com.alibaba.fastjson.JSONArray;
import com.taiji.dianthus.common.BasicController;
import com.taiji.dianthus.common.BusinessMessage;
import com.taiji.dianthus.domain.sms.SmsGroup;
import com.taiji.dianthus.domain.sms.SmsGroupUser;
import com.taiji.dianthus.service.http.SysOrgInfoService;
import com.taiji.dianthus.service.sms.SmsGroupService;
import com.taiji.dianthus.service.sms.SmsGroupUserService;
import com.taiji.dianthus.service.sys.OrgService;
import com.taiji.dianthus.service.sys.SysLogService;
import com.taiji.dianthus.util.IPUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.taiji.dianthus.common.Constant.LOG_OPERATION_DEL;
import static com.taiji.dianthus.common.Constant.LOG_OPERATION_QUERY;
import static com.taiji.dianthus.common.Constant.LOG_OPERATION_SAVE;

/**
 * @ClassName SmsGroupViewController
 * @Description
 * @Author H.M 用户群组功能
 * @Date 2020/7/15
 */
@RequestMapping(value = "/sms/groupUser")
@Controller
public class SmsGroupUserViewController extends BasicController {

    @Autowired
    private SysLogService sysLogService;

    @Autowired
    private SmsGroupUserService groupUserService;

    @Autowired
    private OrgService orgService;
    @Autowired
    private SmsGroupService goupService;


    /**
     * 群组列表页面
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/list")
    public String sendMessageIndex(Model model, HttpServletRequest request, Integer pageSize, Integer currentPage, String groupId, String userName, String userPhone, String groupCurrentPage) {
        pageSize = getPageSize(pageSize);
        currentPage = getCurrentPage(currentPage);
        StringBuilder url = new StringBuilder("/sms/groupUser/list?pagSize=10&groupId=" + groupId);
        String groupUrl = "/sms/group/list?pagSize=10";
        if (groupCurrentPage != null) {
            groupUrl += ("&currentPage=" + groupCurrentPage);
            model.addAttribute("groupCurrentPage", groupCurrentPage);
        }
        if (currentPage != null) {
            model.addAttribute("currentPage", currentPage + 1);
        }
        if (!StringUtils.isEmpty(userName) && !"".equals(userName)) {
            userName = userName.trim();
            url.append("&userName=" + userName);
            model.addAttribute("userName", userName);
        }
        if (!StringUtils.isEmpty(userPhone) && !"".equals(userPhone)) {
            userPhone = userPhone.trim();
            url.append("&userPhone=" + userPhone);
            model.addAttribute("userPhone", userPhone);
        }
        BusinessMessage message = groupUserService.listAll(currentPage, pageSize, groupId, userName, userPhone);
        SmsGroup group = goupService.getOneById(groupId);
        model.addAttribute("group", group);
        model.addAttribute("data", message.getData());
        model.addAttribute("currentUser", currentUser());
        model.addAttribute("menus", listMenu());
        model.addAttribute("url", url);
        model.addAttribute("groupId", groupId);
        model.addAttribute("groupUrl", groupUrl);
        sysLogService.save(currentUser(), LOG_OPERATION_QUERY, IPUtil.getIpAddr(request),
                "查询群组用户列表", message.success ? 1 : 0, message.getMsg());
        return "/pages/sms/groupUserList";
    }

    /**
     * 跳转添加用户
     */
    @RequestMapping(value = "/add")
    public String addGroupUser(Model model, HttpServletRequest request, String groupId, String groupCurrentPage, String flag, String smsId) {
        if (!StringUtils.isEmpty(flag) && !"".equals(flag) && "groupSend".equals(flag)) {//判断跳转群发页面还是发送页面
            String url = "/tjsms/sms/sendGroup/sendIndex?groupId=" + groupId + "&id=" + smsId + "&flag=" + flag;
            model.addAttribute("url", url);
        } else {
            String url = "/tjsms/sms/groupUser/list?pagSize=10" + "&groupId=" + groupId;
            if (groupCurrentPage != null) {
                url += ("&groupCurrentPage=" + groupCurrentPage);
                model.addAttribute("groupCurrentPage", groupCurrentPage);
            }
            model.addAttribute("url", url);
        }
        SmsGroup group = goupService.getOneById(groupId);
        model.addAttribute("group", group);
        List<SmsGroupUser> groupUserList = groupUserService.getAllUserListByGroupIdBooks(groupId);
        JSONArray tree = orgService.getOrgUserTree();
        model.addAttribute("tree", tree);
        model.addAttribute("data", groupUserList);
        model.addAttribute("currentUser", currentUser());
        model.addAttribute("menus", listMenu());
        model.addAttribute("flag", flag);
        model.addAttribute("groupId", groupId);
        sysLogService.save(currentUser(), LOG_OPERATION_QUERY, IPUtil.getIpAddr(request),
                "跳转添加通讯录用户页面", 1, "跳转添加通讯录用户页面");
        return "/pages/sms/groupUserAdd";
    }

    @ResponseBody
    @RequestMapping("/save")
    public BusinessMessage saveGroupUser(HttpServletRequest request, @RequestBody List<SmsGroupUser> groupUsers) {
        BusinessMessage message = groupUserService.save(groupUsers);
        String url = "/sms/groupUser/list?pagSize=10" + "&groupId=" + groupUsers.get(0).getGroupId();
        message.setUrl(url);
        sysLogService.save(currentUser(), LOG_OPERATION_SAVE,
                IPUtil.getIpAddr(request), "添加群组用户", message.success ? 1 : 0, message.getMsg());
        return message;
    }

    @ResponseBody
    @RequestMapping("/saveOne")
    public BusinessMessage saveOneGroupUser(HttpServletRequest request, @RequestBody SmsGroupUser groupUser) {
        groupUser.setUserFlag(1);
        BusinessMessage message = groupUserService.saveOneGroupUser(groupUser);
        String url = "/sms/groupUser/list?pagSize=10" + "&groupId=" + groupUser.getGroupId();
        message.setUrl(url);
        sysLogService.save(currentUser(), LOG_OPERATION_SAVE,
                IPUtil.getIpAddr(request), "添加群组自定义用户", message.success ? 1 : 0, message.getMsg());
        return message;
    }

    @ResponseBody
    @RequestMapping("/checkUserPhoneByGroup")
    public BusinessMessage checkUserPhoneByGroup(@RequestParam String groupId, @RequestParam String userPhone) {
        return groupUserService.checkUserPhoneByGroup(groupId, userPhone);
    }

    @ResponseBody
    @RequestMapping("/delete")
    public BusinessMessage delete(HttpServletRequest request, String id) {
        BusinessMessage message = groupUserService.deleteGroupUserById(id);
        sysLogService.save(currentUser(), LOG_OPERATION_DEL,
                IPUtil.getIpAddr(request), "删除群组用户", message.success ? 1 : 0, message.getMsg());
        return message;
    }

    @ResponseBody
    @RequestMapping("/deleteAll")
    public BusinessMessage deleteAll(HttpServletRequest request, String[] ids) {
        BusinessMessage message = groupUserService.deleteGroupUserByIdArrays(ids);
        sysLogService.save(currentUser(), LOG_OPERATION_DEL,
                IPUtil.getIpAddr(request), "批量删除群组用户", message.success ? 1 : 0, message.getMsg());
        return message;
    }

    @ResponseBody
    @RequestMapping("/getAllUserByGroupId")
    public BusinessMessage getAllUserByGroupId(HttpServletRequest request, String groupId) {
        BusinessMessage message = groupUserService.getCountUserByGroupId(groupId);
        sysLogService.save(currentUser(), LOG_OPERATION_QUERY,
                IPUtil.getIpAddr(request), "查询指定群组下用户", message.getData() != null ? 1 : 0, message.getMsg());
        return message;
    }

}
